package cqrtplm.listener;

import cn.hutool.extra.spring.SpringUtil;
import com.hustcad.plm.pdm.workflow.annotion.CustomListener;
import com.hustcad.plm.pdm.workflow.annotion.CustomListenerField;
import com.hustcad.plm.pdm.workflow.model.constant.ProcessConstant;
import cqrtplm.service.RTChgFormDataVerifyService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.flowable.engine.impl.el.FixedValue;

@Slf4j
@CustomListener(category = "润通", description = "变更表单维护校验")
public class RTChangeFormDataVerifyListener implements ExecutionListener {
    @CustomListenerField(description = "节点", defaultValue = "", required = false)
    private FixedValue node;

    @Override
    public void notify(DelegateExecution execution) {
        long start = System.currentTimeMillis();
        String workFlowContainerId = execution.getProcessInstanceBusinessKey();
        log.info("----------润通----------开始变更表单维护校验------workFlowContainerId:{}----->>>", workFlowContainerId);
        if (StringUtils.isBlank(workFlowContainerId)) {
            workFlowContainerId = execution.getVariable(ProcessConstant.WORK_FLOW_CONTAINER_ID).toString();
        }
        String nodeName="";
        if (node!=null){
            nodeName=node.getExpressionText();
        }
        RTChgFormDataVerifyService rtChgFormDataVerifyService = SpringUtil.getBean(RTChgFormDataVerifyService.class);
        rtChgFormDataVerifyService.checkFormData(workFlowContainerId,nodeName);
        log.info("----------变更表单维护校验完成：{}---->>>", System.currentTimeMillis() - start);
    }


}
